package com.example.administrator.personalmail.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.personalmail.R;
import com.example.administrator.personalmail.model.Wares;
import com.example.administrator.personalmail.widget.CartProvider;
import com.facebook.drawee.view.SimpleDraweeView;

import java.security.PublicKey;
import java.util.ArrayList;
import java.util.List;

public class HotAdapter extends RecyclerView.Adapter {
    private List<Wares> mDatas;
    private int NORMAL = 0;
    private int FOOTER = 1;
    private boolean isNeedFooter;
    private Context mContext;

    private CartProvider cartProvider;

    public HotAdapter(List<Wares> wares, Context context){
        mDatas = wares;
        mContext = context;

        cartProvider = new CartProvider(context);
    }

    AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

        }
    };

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       if (viewType==FOOTER && isNeedFooter){
            View footer = LayoutInflater.from(parent.getContext()).inflate(R.layout.no_more,parent,false);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            footer.setLayoutParams(lp);
            return new FooterViewHoder(footer);
        }

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.template_hot_wares,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        if (getItemViewType(position)==FOOTER) return;

        if (getItemViewType(position)==NORMAL){
            final ViewHolder holder = (ViewHolder) viewHolder;
            Wares wares = mDatas.get(position);

            holder.draweeView.setImageURI(Uri.parse(wares.getImgUrl()));
            holder.textTitle.setText(wares.getName());
            holder.textPrice.setText("￥ "+wares.getPrice());

        }


    }

    @Override
    public int getItemViewType(int position) {
        if (position==mDatas.size() && isNeedFooter){
            return FOOTER;
        }
        return NORMAL;
    }



    public void setFOOTER(boolean type){
        isNeedFooter = type;
    }


    public int getSize(){
        return mDatas.size();
    }


    public void clearData(){

        mDatas.clear();
        notifyItemRangeRemoved(0,mDatas.size());
    }

    public void addData(List<Wares> datas){

        addData(0,datas);
    }

    public void addData(int position,List<Wares> datas){

        if(datas !=null && datas.size()>0) {

            mDatas.addAll(datas);
            notifyItemRangeChanged(position, mDatas.size());
        }
    }


    @Override
    public int getItemCount() {
        if (isNeedFooter)
            return mDatas.size()+1;

        return mDatas.size();
    }

    private ItemClickListenner mItemClickListenner;
    public interface ItemClickListenner{
        public void onItemClick(int position);
    }

    public void setOnItemClickListener(ItemClickListenner itemClickListener){
        mItemClickListenner = itemClickListener;
    }


    class ViewHolder extends RecyclerView.ViewHolder{
        SimpleDraweeView draweeView;
        TextView textTitle;
        TextView textPrice;
        Button button;
        public ViewHolder(View itemView) {
            super(itemView);
            draweeView = (SimpleDraweeView) itemView.findViewById(R.id.fvHot);
            textTitle= (TextView) itemView.findViewById(R.id.text_title);
            textPrice= (TextView) itemView.findViewById(R.id.text_price);
            button = itemView.findViewById(R.id.btn_add_hot);

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    cartProvider.put(mDatas.get(getLayoutPosition()));
                    Toast.makeText(mContext,"已添加到购物车",Toast.LENGTH_SHORT).show();
                }
            });

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    Toast.makeText(mContext,getLayoutPosition()+"",Toast.LENGTH_SHORT).show();
                    mItemClickListenner.onItemClick(getLayoutPosition());
                }
            });
        }


    }

    class FooterViewHoder extends RecyclerView.ViewHolder{

        public FooterViewHoder(View itemView) {
            super(itemView);
        }
    }
}
